<?php 


//记录日志
function print_log($msg,$level="INFO"){

    $log_msg=$msg;

    //如果开启 debug ，输出信息
    $open_debug=getConfig("OPEN_DEBUG");

    if($open_debug){

        if(is_string($log_msg)){

            echo date('Y-m-d H:i:s').":[$level]".' >>> '.$log_msg."<br>";
        }

        if(is_array($log_msg)){

            var_dump($log_msg);

            echo "<br>";
        }
    }

	$logFile = __DIR__."/../logs/".date('Y-m-d').'.log';

    if(is_array($log_msg)){

        $log_msg=json_encode($log_msg);
    }


    $log_msg = date('Y-m-d H:i:s').":[$level]".' >>> '.$log_msg."\r\n";

	file_put_contents($logFile,$log_msg,FILE_APPEND);

}


// 获取配置文件中的参数
function getConfig($key){

	 $conf= include __DIR__."/../conf/conf.php";

	 return $conf[$key];
}




//向远程服务器提交post请求
function _post_remote($url,$data){
	
	//生成url-encode后的请求字符串，将数组转换为字符串  
	$data = http_build_query($data);  
	$opts = array (
		'http' => array (
			'method' => 'POST', 
			'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n", 
			'content' => $data
		)  
	);  

	//生成请求的句柄文件  
	$context = stream_context_create($opts);  
	$result = file_get_contents($url, false, $context);  

	return $result;
}


// 使用curl 方式提交数据， 服务器上的php 需开启 php_curl.dll
function _post_remote_curl($url,$data=null){

	$curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}


function _get_remote($url){

	$result="";

	$fp = fopen($url, 'r');  

	stream_get_meta_data($fp); 

	while(!feof($fp)) {  
		$result .= fgets($fp, 1024);  
	}  
	 
	fclose($fp);  

	return $result;
}


function _get_remote_curl($url){

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);

    $output = curl_exec($curl);

    curl_close($curl);

    return $output;

}


